<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">仓库管理系统</h1>
        <h4 style="text-align: center">用户登录</h4>
        <el-form
          :model="loginForm"
          label-width="100px"
          :rules="rules"
          ref="loginForm"
        >
          <el-form-item label="账号" prop="no">
            <el-input
              style="width: 200px"
              type="text"
              v-model="loginForm.no"
              autocomplete="off"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              style="width: 200px"
              type="password"
              v-model="loginForm.password"
              show-password
              autocomplete="off"
              size="small"
              @keyup.enter.native="confirm"
            ></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-input
              style="width: 100px"
              type="text"
              v-model="loginForm.code"
              autocomplete="off"
              size="small"
            ></el-input>
            <img :src="Code" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="confirm"
              :disabled="confirm_disabled"
              >登录</el-button
            >
            <router-link to="/register">去注册</router-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>


<script>
export default {
  name: "Login",
  data() {
    return {
      confirm_disabled: false,
      loginForm: {
        no: "",
        password: "",
        code: "",
      },
      // Code存放验证码
      Code: "",
      rules: {
        no: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输密码", trigger: "blur" }],
        code: [{ required: true, message: "请输验证码", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    // 获取验证码
    getCode() {
      this.$axios.get(this.$httpUrl + "/user/getCode").then((Response) => {
        console.log(Response.data);
        // 给验证码赋值
        this.Code = Response.data.data;
      });
    },
    confirm() {
      this.confirm_disabled = true;
      console.log("页面收参", this.loginForm);
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //valid成功为true，失败为false
          //去后台验证用户名密码
          this.$axios
            .post(
              this.$httpUrl + "/user/login?code=" + this.loginForm.code,
              this.loginForm
            )
            .then((res) => res.data)
            .then((res) => {
              console.log(res);
              if (res.code == 200) {
                //存储
                sessionStorage.setItem(
                  "CurUser",
                  JSON.stringify(res.data.user)
                );

                console.log(res.data.menu);
                this.$store.commit("setMenu", res.data.menu);
                //跳转到主页
                this.$router.replace("/Index");
              } else {
                this.confirm_disabled = false;
                alert("校验失败，用户名,密码或验证码错误！");
                this.loginForm = "";
                return false;
              }
            });
        } else {
          this.confirm_disabled = false;
          console.log("校验失败");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.loginBody {
  background-image: url("index.jpg");
  background-size: 100%;
  position: absolute;
  width: 100%;
  height: 100%;
  /* background-color: #B3C0D1; */
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: rgba(
    255,
    255,
    255,
    0.5
  ); /* 使用 rgba() 函数设置背景色并指定透明度 */
  border-radius: 5%;
}
.login-title {
  margin: 20px 0;
  text-align: center;
}
.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
.login-title {
  font-size: 30px;
}
</style>